﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.IO;
using System.Drawing.Printing;
using System.Drawing.Imaging;

namespace RecruitStudents.Client.Score
{
    public class EMFStreamPrintDocument : System.Drawing.Printing.PrintDocument
    {

        private int m_CurrentPageIndex;
        private IList<Stream> m_EMFStreams;
        private Microsoft.Reporting.WinForms.LocalReport m_LocalReport = null;
        private System.Drawing.Imaging.Metafile m_PageImage = null;
        private string m_ReportName = string.Empty;
        private bool bolOrientation = false;

        //构造函数
        public EMFStreamPrintDocument(Microsoft.Reporting.WinForms.LocalReport localReport, string ReportName)
            : base()
        {
            this.m_ReportName = ReportName;

            ReadSetting();

            this.m_LocalReport = localReport;

        }


        //读取配制文件信息
        private EMFDeviceInfo ReadSetting()
        {
            System.Data.DataSet dsXML = new DataSet();
            System.Data.DataTable dtXML = new DataTable();
            EMFDeviceInfo emfdi = new EMFDeviceInfo();

            dsXML.ReadXml(System.Windows.Forms.Application.StartupPath + @"\ReportSettings.xml");

            foreach (System.Data.DataTable dt in dsXML.Tables)
            {
                if (dt.TableName == this.m_ReportName)
                {
                    dtXML = dt;
                    break;
                }
            }

            if (dtXML.Rows.Count != 0)
            {
                System.Data.DataRow dr = dtXML.Rows[0];

                emfdi.PageWidth = System.Convert.ToDecimal(dr["PageWidth"].ToString());
                emfdi.PageHeight = System.Convert.ToDecimal(dr["PageHeight"].ToString());
                emfdi.MarginTop = System.Convert.ToDecimal(dr["MarginTop"].ToString());
                emfdi.MarginBottom = System.Convert.ToDecimal(dr["MarginBottom"].ToString());
                emfdi.MarginLeft = System.Convert.ToDecimal(dr["MarginLeft"].ToString());
                emfdi.MarginRight = System.Convert.ToDecimal(dr["MarginRight"].ToString());

                if (dr["Orientation"].ToString() == "V")
                {
                    base.DefaultPageSettings.Landscape = false;
                    base.PrinterSettings.DefaultPageSettings.Landscape = false;
                    emfdi.Landscape = false;
                }
                else
                {
                    base.DefaultPageSettings.Landscape = true;
                    base.PrinterSettings.DefaultPageSettings.Landscape = true;
                    emfdi.Landscape = true;
                }

                this.bolOrientation = emfdi.Landscape;


                if (dtXML != null)
                    dtXML.Dispose();
                if (dsXML != null)
                {
                    dsXML.Clear();
                    dsXML = null;
                }

                return emfdi;
            }

            return null;
        }


        private Stream CreateStream(string Name, string FileNameExtension, Encoding Encoding, string MimeType, bool WillSeek)
        {
            System.IO.Stream streamRet = new System.IO.MemoryStream();
            this.m_EMFStreams.Add(streamRet);
            return streamRet;
        }


        private void Export(LocalReport Report)
        {

            EMFDeviceInfo emfdi = this.ReadSetting();

            string strDeviceInfo = emfdi.DeviceInfoString;

            emfdi = null;


            Microsoft.Reporting.WinForms.Warning[] Warnings;

            this.m_EMFStreams = new System.Collections.Generic.List<System.IO.Stream>();

            Report.Render("Image", strDeviceInfo, this.CreateStream, out Warnings);

            foreach (System.IO.Stream s in this.m_EMFStreams)
                s.Position = 0;


        }


        protected override void OnBeginPrint(PrintEventArgs ev)
        {
            base.OnBeginPrint(ev);
        }


        protected override void OnPrintPage(PrintPageEventArgs ev)
        {
            base.OnPrintPage(ev);

            if (this.m_EMFStreams == null || this.m_EMFStreams.Count == 0)
            {
                this.Export(this.m_LocalReport);
                this.m_CurrentPageIndex = 0;
                if (this.m_EMFStreams.Count == 0 || this.m_EMFStreams == null)
                    return;
            }

            this.m_PageImage = new Metafile(this.m_EMFStreams[this.m_CurrentPageIndex]);

            ev.Graphics.DrawImageUnscaledAndClipped(this.m_PageImage, ev.PageBounds);

            this.m_CurrentPageIndex++;

            ev.HasMorePages = (this.m_CurrentPageIndex < this.m_EMFStreams.Count);

            if (this.m_CurrentPageIndex == this.m_EMFStreams.Count)
            {
                this.m_CurrentPageIndex = 0;
                this.m_EMFStreams.Clear();
                this.m_EMFStreams = null;
                this.m_PageImage.Dispose();
                base.Dispose();
            }

        }


    }

}
